Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement very minimalistic support for go workspaces #1250

Merged
merged 7 commits into from
Jun 12, 2022

Conversation

HakanSunay
Copy link
Contributor

@HakanSunay HakanSunay commented May 9, 2022

With this change, gazelle can invoke update-repos by processing go.work files.

Most of the common logic in module.go and work.go has been extracted into utils.go.
The main difference between the module and workspace mode lies in the action for fetching module sums.
For modules, only the missing sums are fetched through go mod download, but for workspaces the whole process is delegated to go mod download. This is due to the fact that go list does not show module sums yet. An upstream proposal was filed for the same, which can be seen in golang/go#52792.

Given the fact the standard go tooling in 1.18 can work with go.work files, not many changes were actually necessary.
A smoke test was also added to update_import_test.go to verify that the latest version of govmomi and rest is always picked.

How to invoke:

gazelle update-repos -from_file=go.work -to_macro=deps.bzl%go_deps -prune=True

What type of PR is this?
Feature

What package or component does this PR mostly affect?
language/go

What does this PR do? Why is it needed?
Implements minimalistic support for GoLang workspaces. It would help folks with multiple go.mod files in the monorepo.

Which issues(s) does this PR fix?

Fixes #1232

Other notes for review

Please, also do comment if we should be invoking go work sync as part of this workflow, which will sync all the child modules. Currently, this has not been implemented for the sake of not introducing any side effects to the execution of gazelle with regards to go.mod file changes.

I am open to testing suggestions and other possible improvements, that I might have missed.

With this change gazelle can invoke `update-repos` by processing `go.work` files.
The basis of the change is a plain copy-paste of `update.go` into `work.go`.
However, since `go list` does not show module sums, the whole process is delegated to `go mod download`.
golang/go#52792 was created to track the above.

The standard go tooling in 1.18 can work with `go.work` files so not many changes were actually necessary.
A smoke test is also added to `update_import_test.go` to verify that the latest version of govmomi and rest is always picked.

How to invoke:
$ gazelle update-repos -from_file=go.work -to_macro=deps.bzl%go_deps -prune=True
@sluongng
Copy link
Contributor

sluongng commented May 13, 2022

This approach makes sense to me, though I wonder if we should mandate a go work sync call prior to generating things?

Could you please make sure this PR pass CI? It would help reviewing this a lot easier.

@HakanSunay
Copy link
Contributor Author

This approach makes sense to me, though I wonder if we should mandate a go work sync call prior to generating things?

Could you please make sure this PR pass CI? It would help reviewing this a lot easier.

CI is green now.
I also thought about running go work sync and was wondering if gazelle executions should be causing any side effects to the submodules (their go.mod and go.sum files).

Copy link
Contributor

@sluongng sluongng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some small nits, but overall this is looking real good.

There are obvious code duplication between work.go and modules.go that I wish we could apply some DRY... but perhaps YAGNI.

language/go/work.go Outdated Show resolved Hide resolved
language/go/work.go Outdated Show resolved Hide resolved
@sluongng
Copy link
Contributor

This approach makes sense to me, though I wonder if we should mandate a go work sync call prior to generating things?
Could you please make sure this PR pass CI? It would help reviewing this a lot easier.

CI is green now. I also thought about running go work sync and was wondering if gazelle executions should be causing any side effects to the submodules (their go.mod and go.sum files).

fair point.

May be add some documentation on this feature, then reminds people to run go work sync in the doc?

@HakanSunay
Copy link
Contributor Author

I have some small nits, but overall this is looking real good.

There are obvious code duplication between work.go and modules.go that I wish we could apply some DRY... but perhaps YAGNI.

You are absolutely right, I think we should do it properly from the start (DRY). I have attempted to provide a more unified approach without that much code repetition for both modes, you can have a look.

Copy link
Contributor

@sluongng sluongng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nits, but the rest LGTM.

Could you please update existing doc to add Go Workspace support

Once thats done, just remove draft status for other maintainers to review.

language/go/work.go Outdated Show resolved Hide resolved
language/go/modules.go Outdated Show resolved Hide resolved
@HakanSunay HakanSunay changed the title Implement very minimalistic support go workspaces Implement very minimalistic support for go workspaces May 16, 2022
@HakanSunay HakanSunay marked this pull request as ready for review May 16, 2022 17:15
@sluongng
Copy link
Contributor

sluongng commented Jun 1, 2022

@linzhp @achew22 I was able to apply this patch in my repo and have things working as expected sluongng/nogo-analyzer@4769fa6

Could you guys take a look and consider merging this?

@tomqwpl
Copy link

tomqwpl commented Jun 10, 2022

This PR explains some things I am confused about with gazelle.
Bazel is supposed to be a tool for mono repos right? In a mono repo, I'm going to have lots of directories, each having a go.mod file. So far I've been confused by how I set this up, it looks like I have to set up targets in each of the modules to run the update-repos separately for each module, where running plain gazelle can be done over the whole thing from a BUILD.bazel file at the root.

So just for clarification, I have:

WORKSPACE
BUILD.bazel
module1\
   go.mod
   BUILD.bazel
module2
  go.mod
  BUILD.bazel

The BUILD.bazel file at the root has

# gazelle:prefix example.com/example
gazelle(name = "gazelle")
```

That appears to take care of generating the BUILD.bazel files for all the modules and their subdirectories. The bit I'm confused about is that I had naively tried adding 

gazelle(
name = "gazelle-update-repos",
args = [
"-from_file=go.mod",
"-to_macro=deps.bzl%go_dependencies",
"-prune",
],
command = "update-repos",
)

to the same root BUILD.bazel file as well, but it appears that I have to put that individually into the BUILD.bazel file next to each go.mod file. I'm completely new to bazel, so maybe this shouldn't have been, but it felt like I should have to run that update-repos once across the whole monorepo.

Hopefully this PR helps that?

@sluongng
Copy link
Contributor

@tomqwpl I think you are conflating some definitions here. This PR provide support for go.work file, which is Go 1.18's new workspace feature.

If you have a hard time learning Bazel/Gazelle, I would suggest using Bazel's Community Slack or StackOverFlow to ask questions.

Copy link
Contributor

@linzhp linzhp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested in Uber's repo, this doesn't break existing gazelle update-repo -from_file=go.mod.

@linzhp linzhp merged commit 0e810ae into bazel-contrib:master Jun 12, 2022
gcf-merge-on-green bot referenced this pull request in googleapis/gapic-config-validator Jun 27, 2022
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [bazel_gazelle](https://togithub.com/bazelbuild/bazel-gazelle) | http_archive | minor | `v0.25.0` -> `v0.26.0` |

---

### Release Notes

<details>
<summary>bazelbuild/bazel-gazelle</summary>

### [`v0.26.0`](https://togithub.com/bazelbuild/bazel-gazelle/releases/tag/v0.26.0)

[Compare Source](https://togithub.com/bazelbuild/bazel-gazelle/compare/v0.25.0...v0.26.0)

#### What's Changed

-   fix(tests): fix gazelle_generation_test expected stderr update by [@&#8203;jbedard](https://togithub.com/jbedard) in [https://github.com/bazelbuild/bazel-gazelle/pull/1220](https://togithub.com/bazelbuild/bazel-gazelle/pull/1220)
-   Add an e2e test confirming no output on success by [@&#8203;achew22](https://togithub.com/achew22) in [https://github.com/bazelbuild/bazel-gazelle/pull/1216](https://togithub.com/bazelbuild/bazel-gazelle/pull/1216)
-   Update extend.md with a practical languages example by [@&#8203;Anthony-Bible](https://togithub.com/Anthony-Bible) in [https://github.com/bazelbuild/bazel-gazelle/pull/1222](https://togithub.com/bazelbuild/bazel-gazelle/pull/1222)
-   fix: Remove gazelle_binary import collision by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1226](https://togithub.com/bazelbuild/bazel-gazelle/pull/1226)
-   Broaden label name regex by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1229](https://togithub.com/bazelbuild/bazel-gazelle/pull/1229)
-   gazelle_generation_test: redact workspace path from output by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1231](https://togithub.com/bazelbuild/bazel-gazelle/pull/1231)
-   Add -print0 to print names of rewritten files by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1213](https://togithub.com/bazelbuild/bazel-gazelle/pull/1213)
-   Code Quality Improvements by [@&#8203;sluongng](https://togithub.com/sluongng) in [https://github.com/bazelbuild/bazel-gazelle/pull/1197](https://togithub.com/bazelbuild/bazel-gazelle/pull/1197)
-   Add -strict to exit on build file and directive errors by [@&#8203;dr-dime](https://togithub.com/dr-dime) in [https://github.com/bazelbuild/bazel-gazelle/pull/1214](https://togithub.com/bazelbuild/bazel-gazelle/pull/1214)
-   fix(lang/proto): include imports from different targets by [@&#8203;nickgooding](https://togithub.com/nickgooding) in [https://github.com/bazelbuild/bazel-gazelle/pull/1237](https://togithub.com/bazelbuild/bazel-gazelle/pull/1237)
-   Update rules example in README to v0.25.0 by [@&#8203;yujunz](https://togithub.com/yujunz) in [https://github.com/bazelbuild/bazel-gazelle/pull/1240](https://togithub.com/bazelbuild/bazel-gazelle/pull/1240)
-   Allow static dependency resolution for Gazelle rule by [@&#8203;uhthomas](https://togithub.com/uhthomas) in [https://github.com/bazelbuild/bazel-gazelle/pull/1242](https://togithub.com/bazelbuild/bazel-gazelle/pull/1242)
-   Handle wrapped errors by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1234](https://togithub.com/bazelbuild/bazel-gazelle/pull/1234)
-   Go: Update tests to use cmp.Diff instead of reflect.DeepEqual by [@&#8203;thempatel](https://togithub.com/thempatel) in [https://github.com/bazelbuild/bazel-gazelle/pull/1244](https://togithub.com/bazelbuild/bazel-gazelle/pull/1244)
-   Fix startup script manifest resolution with --nolegacy_external_runfiles by [@&#8203;jvolkman](https://togithub.com/jvolkman) in [https://github.com/bazelbuild/bazel-gazelle/pull/1247](https://togithub.com/bazelbuild/bazel-gazelle/pull/1247)
-   Label's package may contain [@&#8203;s](https://togithub.com/s) by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1249](https://togithub.com/bazelbuild/bazel-gazelle/pull/1249)
-   Trim runfiles prefix consistently by [@&#8203;uhthomas](https://togithub.com/uhthomas) in [https://github.com/bazelbuild/bazel-gazelle/pull/1257](https://togithub.com/bazelbuild/bazel-gazelle/pull/1257)
-   Respect .bazelignore by [@&#8203;Whoaa512](https://togithub.com/Whoaa512) in [https://github.com/bazelbuild/bazel-gazelle/pull/1245](https://togithub.com/bazelbuild/bazel-gazelle/pull/1245)
-   Implement very minimalistic support for go workspaces by [@&#8203;HakanSunay](https://togithub.com/HakanSunay) in [https://github.com/bazelbuild/bazel-gazelle/pull/1250](https://togithub.com/bazelbuild/bazel-gazelle/pull/1250)
-   Fix typo in comment by [@&#8203;yujunz](https://togithub.com/yujunz) in [https://github.com/bazelbuild/bazel-gazelle/pull/1270](https://togithub.com/bazelbuild/bazel-gazelle/pull/1270)
-   Use `patch` from `@bazel_tools//tools/build_defs/repo:utils.bzl` by [@&#8203;bozaro](https://togithub.com/bozaro) in [https://github.com/bazelbuild/bazel-gazelle/pull/1269](https://togithub.com/bazelbuild/bazel-gazelle/pull/1269)
-   Update rules_go to 0.33.0 by [@&#8203;fmeum](https://togithub.com/fmeum) in [https://github.com/bazelbuild/bazel-gazelle/pull/1263](https://togithub.com/bazelbuild/bazel-gazelle/pull/1263)
-   Add support for auth_patterns in go_repository by [@&#8203;dmivankov](https://togithub.com/dmivankov) in [https://github.com/bazelbuild/bazel-gazelle/pull/1254](https://togithub.com/bazelbuild/bazel-gazelle/pull/1254)
-   Sluongng/revert patch by [@&#8203;sluongng](https://togithub.com/sluongng) in [https://github.com/bazelbuild/bazel-gazelle/pull/1277](https://togithub.com/bazelbuild/bazel-gazelle/pull/1277)
-   Stop inferring import path for empty packages by [@&#8203;linzhp](https://togithub.com/linzhp) in [https://github.com/bazelbuild/bazel-gazelle/pull/1280](https://togithub.com/bazelbuild/bazel-gazelle/pull/1280)
-   Don't exclude spaces from the label name regex by [@&#8203;illicitonion](https://togithub.com/illicitonion) in [https://github.com/bazelbuild/bazel-gazelle/pull/1271](https://togithub.com/bazelbuild/bazel-gazelle/pull/1271)

#### New Contributors

-   [@&#8203;Anthony-Bible](https://togithub.com/Anthony-Bible) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1222](https://togithub.com/bazelbuild/bazel-gazelle/pull/1222)
-   [@&#8203;dr-dime](https://togithub.com/dr-dime) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1231](https://togithub.com/bazelbuild/bazel-gazelle/pull/1231)
-   [@&#8203;sluongng](https://togithub.com/sluongng) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1197](https://togithub.com/bazelbuild/bazel-gazelle/pull/1197)
-   [@&#8203;nickgooding](https://togithub.com/nickgooding) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1237](https://togithub.com/bazelbuild/bazel-gazelle/pull/1237)
-   [@&#8203;yujunz](https://togithub.com/yujunz) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1240](https://togithub.com/bazelbuild/bazel-gazelle/pull/1240)
-   [@&#8203;uhthomas](https://togithub.com/uhthomas) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1242](https://togithub.com/bazelbuild/bazel-gazelle/pull/1242)
-   [@&#8203;thempatel](https://togithub.com/thempatel) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1244](https://togithub.com/bazelbuild/bazel-gazelle/pull/1244)
-   [@&#8203;Whoaa512](https://togithub.com/Whoaa512) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1245](https://togithub.com/bazelbuild/bazel-gazelle/pull/1245)
-   [@&#8203;HakanSunay](https://togithub.com/HakanSunay) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1250](https://togithub.com/bazelbuild/bazel-gazelle/pull/1250)
-   [@&#8203;bozaro](https://togithub.com/bozaro) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1269](https://togithub.com/bazelbuild/bazel-gazelle/pull/1269)
-   [@&#8203;fmeum](https://togithub.com/fmeum) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1263](https://togithub.com/bazelbuild/bazel-gazelle/pull/1263)
-   [@&#8203;dmivankov](https://togithub.com/dmivankov) made their first contribution in [https://github.com/bazelbuild/bazel-gazelle/pull/1254](https://togithub.com/bazelbuild/bazel-gazelle/pull/1254)

**Full Changelog**: bazel-contrib/bazel-gazelle@v0.25.0...v0.26.0

#### `WORKSPACE` code

    load("@&#8203;bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

    http_archive(
        name = "bazel_gazelle",
        sha256 = "501deb3d5695ab658e82f6f6f549ba681ea3ca2a5fb7911154b5aa45596183fa",
        urls = [
            "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
            "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.26.0/bazel-gazelle-v0.26.0.tar.gz",
        ],
    )

    load("@&#8203;bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

    ############################################################

### Define your own dependencies here using go_repository.

### Else, dependencies declared by rules_go/gazelle will be used.

### The first declaration of an external repository "wins".

    ############################################################

    gazelle_dependencies()

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/googleapis/gapic-config-validator).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for go 1.18 "workspace" mode?
4 participants